


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en">
<!--<![endif]-->

<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>hfai.client.api.custom &mdash; HAI Platform  documentation</title>
  

  <link rel="shortcut icon" href="../../../../_static/images/logo192.png" />
  
  

  

  
  
  

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <!-- <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" /> -->
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/element-plus.css" type="text/css" />
  <link rel="index" title="Index" href="../../../../genindex.html" />
  <link rel="search" title="Search" href="../../../../search.html" />
  <!-- Google Analytics -->
  <script type="text/javascript">
    var collapsedSections = [];
  </script>
  
  <!-- End Google Analytics -->
  

  
  <script src="../../../../_static/js/modernizr.min.js"></script>
  <script>
    MathJax = {
        chtml: {
            scale: 1,
            minScale: 1,
        },
        svg: {
            scale: 1,
            minScale: 1,
        }
    }
</script>

  <!-- Preload the theme fonts -->

<link rel="preload" href="../../../../_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

<!-- Preload the katex fonts -->

<link rel="preload" href="../../../../_static/external/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../../../../_static/external/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
  <link rel="stylesheet" href="../../../../_static/external/all.css"
    integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
</head>

<div class="container-fluid header-holder tutorials-header" id="header-holder">
  <div class="container">
    <div class="header-container">
      <a class="header-logo" href=""
        aria-label="OpenMMLab"></a>

      <div class="main-menu">
        <ul>
        </ul>
      </div>

      <!-- <a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a> -->
    </div>
  </div>
</div>

<body class="pytorch-body">

   

  

  <div class="table-of-contents-link-wrapper">
    <span>Table of Contents</span>
    <a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
  </div>

  <nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
    <div class="pytorch-side-scroll">
      <div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
        <div class="pytorch-left-menu-search">
          

          
          
          
          

          



<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search Docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        
        
        
        
        
        <p class="caption" role="heading"><span class="caption-text">开始使用</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../start/hai_intro.html">基本介绍</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../start/install.html">安装与设置</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../start/studio.html">Studio</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">用户须知</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../guide/tutorial.html">命令行工具 (CLI)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../guide/environment.html">环境配置</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../guide/schedule.html">分时调度</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">CLI 说明</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cli/user.html">用户命令</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../cli/exec.html">运行命令</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../cli/task.html">任务命令</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../cli/cluster.html">集群命令</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../cli/ugc.html">UGC 命令</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">API 说明</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/hai.html">hfai</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/client.html">hfai.client</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/client_remote.html">hfai.client.remote</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">其他</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../misc/env_var.html">环境变量</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../misc/resources.html">外部资源</a></li>
</ul>

        
        
      </div>
    </div>
  </nav>

  <div class="pytorch-container">
    <div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
      <div class="pytorch-breadcrumbs-wrapper">
        















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="pytorch-breadcrumbs">
    
      <li>
        <a href="../../../../index.html">
            Docs
        </a> &gt;
      </li>

        
          <li><a href="../../../index.html">Module code</a> &gt;</li>
        
      <li>hfai.client.api.custom</li>
    
    
      <li class="pytorch-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
</div>
      </div>

      <div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
        Shortcuts
      </div>
    </div>

    <section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
      <div class="pytorch-content-left">
        
          <div class="rst-content">
            
            <div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
              <article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
                
  <h1>Source code for hfai.client.api.custom</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timedelta</span>
<span class="kn">from</span> <span class="nn">.api_config</span> <span class="kn">import</span> <span class="n">get_mars_token</span> <span class="k">as</span> <span class="n">mars_token</span>
<span class="kn">from</span> <span class="nn">.api_config</span> <span class="kn">import</span> <span class="n">get_mars_url</span> <span class="k">as</span> <span class="n">mars_url</span>
<span class="kn">from</span> <span class="nn">.training_api</span> <span class="kn">import</span> <span class="n">node_name</span><span class="p">,</span> <span class="n">task_id</span><span class="p">,</span> <span class="n">rank</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">from</span> <span class="nn">traceback</span> <span class="kn">import</span> <span class="n">format_exception</span>
<span class="kn">from</span> <span class="nn">.experiment_api</span> <span class="kn">import</span> <span class="n">create_experiment</span>


<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pynvml</span>
    <span class="n">no_pynvml</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">except</span><span class="p">:</span>
    <span class="n">no_pynvml</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">IS_SIMULATE</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;HFAI_SIMULATE&#39;</span><span class="p">,</span> <span class="s1">&#39;0&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;1&#39;</span>


<span class="k">def</span> <span class="nf">_check_ib</span><span class="p">():</span>
    <span class="n">ib_state_path</span> <span class="o">=</span> <span class="s1">&#39;/sys/class/infiniband/mlx5_0/ports/1/state&#39;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">ib_state_path</span><span class="p">):</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">ib_state_path</span><span class="p">)</span> <span class="k">as</span> <span class="n">isf</span><span class="p">:</span>
            <span class="n">status</span> <span class="o">=</span> <span class="n">isf</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">status</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">status</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;4&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;ib check failed [</span><span class="si">{</span><span class="n">status</span><span class="si">}</span><span class="s1">]&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">_check_ecc</span><span class="p">():</span>
    <span class="c1"># cpu 机器直接返回</span>
    <span class="k">if</span> <span class="n">no_pynvml</span> <span class="ow">or</span> <span class="p">(</span><span class="ow">not</span> <span class="n">node_name</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;dl&#39;</span><span class="p">)):</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="n">pynvml</span><span class="o">.</span><span class="n">nvmlInit</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">ecc_count</span><span class="p">(</span><span class="n">_handle</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pynvml</span><span class="o">.</span><span class="n">nvmlDeviceGetTotalEccErrors</span><span class="p">(</span>
            <span class="n">_handle</span><span class="p">,</span>
            <span class="n">pynvml</span><span class="o">.</span><span class="n">NVML_MEMORY_ERROR_TYPE_UNCORRECTED</span><span class="p">,</span>
            <span class="n">pynvml</span><span class="o">.</span><span class="n">NVML_AGGREGATE_ECC</span><span class="p">)</span>

    <span class="n">device_count</span> <span class="o">=</span> <span class="n">pynvml</span><span class="o">.</span><span class="n">nvmlDeviceGetCount</span><span class="p">()</span>
    <span class="k">if</span> <span class="s1">&#39;CUDA_VISIBLE_DEVICES&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span>  <span class="c1"># 这样会很快</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;CUDA_VISIBLE_DEVICES&#39;</span><span class="p">]:</span> <span class="c1"># 如果没有设定</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;CUDA_VISIBLE_DEVICES&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">device_count</span><span class="p">:</span>
                    <span class="n">handle</span> <span class="o">=</span> <span class="n">pynvml</span><span class="o">.</span><span class="n">nvmlDeviceGetHandleByIndex</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">ecc_count</span><span class="p">(</span><span class="n">handle</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="k">return</span> <span class="kc">False</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">device_count</span><span class="p">):</span>
            <span class="n">handle</span> <span class="o">=</span> <span class="n">pynvml</span><span class="o">.</span><span class="n">nvmlDeviceGetHandleByIndex</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ecc_count</span><span class="p">(</span><span class="n">handle</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>


<span class="k">def</span> <span class="nf">_parse_kern_time</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">year</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[:</span><span class="mi">3</span><span class="p">])</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">&quot;%Y %b </span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span>  <span class="c1"># 如果解析不了，默认时间为一天之前，也就是忽略本条</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_magic_filter</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
    <span class="k">if</span> <span class="s1">&#39;Xid&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">for</span> <span class="n">magic_n</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;63&#39;</span><span class="p">,</span> <span class="s1">&#39;64&#39;</span><span class="p">,</span> <span class="s1">&#39;45&#39;</span><span class="p">,</span> <span class="s1">&#39;43&#39;</span><span class="p">,</span> <span class="s1">&#39;31&#39;</span><span class="p">]:</span>
        <span class="k">if</span> <span class="sa">f</span><span class="s1">&#39;: </span><span class="si">{</span><span class="n">magic_n</span><span class="si">}</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
    <span class="k">return</span> <span class="kc">True</span>


<span class="k">def</span> <span class="nf">_check_kern</span><span class="p">():</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/var/log/kern/kern.log&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">total_len</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">offset</span> <span class="o">=</span> <span class="mi">50</span>  <span class="c1"># 本次看的长度</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">total_len</span> <span class="o">&lt;=</span> <span class="n">offset</span><span class="p">:</span>  <span class="c1"># 看的长度不应该超过总长度</span>
                <span class="n">offset</span> <span class="o">=</span> <span class="n">total_len</span>
            <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="o">-</span><span class="n">offset</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">rst</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">rst</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>  <span class="c1"># 存在完整的一行</span>
                <span class="n">first_idx</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="n">second_idx</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">first_idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">_parse_kern_time</span><span class="p">(</span><span class="n">rst</span><span class="p">[</span><span class="n">first_idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span> <span class="n">second_idx</span><span class="p">]))</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">:</span>  <span class="c1"># 看的内容已经超过5分钟了</span>
                    <span class="k">break</span>
            <span class="k">if</span> <span class="n">total_len</span> <span class="o">==</span> <span class="n">offset</span><span class="p">:</span>  <span class="c1"># 看完了</span>
                <span class="k">break</span>
            <span class="n">offset</span> <span class="o">*=</span> <span class="mi">2</span>  <span class="c1"># 下次看的长度翻倍</span>

        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">rst</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)):</span>  <span class="c1"># 所有行都拎出来看下</span>
            <span class="k">if</span> <span class="s1">&#39;NVRM&#39;</span> <span class="ow">in</span> <span class="n">line</span> <span class="ow">and</span> <span class="n">_magic_filter</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">_parse_kern_time</span><span class="p">(</span><span class="n">line</span><span class="p">))</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;该机器的kern log报了NVRM错误，尝试重启，kern 日志如下：&#39;</span><span class="p">)</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="k">return</span> <span class="p">{</span>
                    <span class="s1">&#39;passed&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                    <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="s1">&#39;kern log存在NVRM; &#39;</span>
                <span class="p">}</span>
            <span class="k">if</span> <span class="s1">&#39;check_quota_exceeded&#39;</span> <span class="ow">in</span> <span class="n">line</span> <span class="ow">and</span> <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">_parse_kern_time</span><span class="p">(</span><span class="n">line</span><span class="p">))</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;该机器的kern log报了ceph quota exceeded，尝试重启，kern 日志如下：&#39;</span><span class="p">)</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="k">return</span> <span class="p">{</span>
                    <span class="s1">&#39;passed&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                    <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="s1">&#39;ceph quota exceeded; &#39;</span>
                <span class="p">}</span>
    <span class="k">return</span> <span class="p">{</span>
        <span class="s1">&#39;passed&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
        <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="s1">&#39;&#39;</span>
    <span class="p">}</span>


<div class="viewcode-block" id="self_health_check"><a class="viewcode-back" href="../../../../api/client.html#hfai.client.self_health_check">[docs]</a><span class="k">def</span> <span class="nf">self_health_check</span><span class="p">(</span><span class="n">pid</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    对当前机器做系统检查，检查通过会退出该任务，检查失败会重启该任务</span>
<span class="sd">    :param pid:</span>
<span class="sd">    :return:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;MARSV2_TASK_TYPE&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;training&#39;</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">hw_check_pass</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">err_msg</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_ib</span><span class="p">():</span>
        <span class="n">hw_check_pass</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">err_msg</span> <span class="o">+=</span> <span class="s1">&#39;ib 报错; &#39;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_ecc</span><span class="p">():</span>
            <span class="n">hw_check_pass</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">err_msg</span> <span class="o">+=</span> <span class="s1">&#39;ecc报错; &#39;</span>
    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="n">hw_check_pass</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">err_msg</span> <span class="o">+=</span> <span class="sa">f</span><span class="s1">&#39;ecc检查出错，请管理员检查: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s1">; &#39;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">rst</span> <span class="o">=</span> <span class="n">_check_kern</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">rst</span><span class="p">[</span><span class="s1">&#39;passed&#39;</span><span class="p">]:</span>
            <span class="n">hw_check_pass</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">err_msg</span> <span class="o">+=</span> <span class="n">rst</span><span class="p">[</span><span class="s1">&#39;msg&#39;</span><span class="p">]</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span>
        <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">mars_url</span><span class="p">()</span><span class="si">}</span><span class="s1">/operating/fail_task?token=</span><span class="si">{</span><span class="n">mars_token</span><span class="p">()</span><span class="si">}</span><span class="s1">&amp;id=</span><span class="si">{</span><span class="n">task_id</span><span class="p">()</span><span class="si">}</span><span class="s1">&amp;rank=</span><span class="si">{</span><span class="n">rank</span><span class="p">()</span><span class="si">}</span><span class="s1">&amp;hw_check_pass=</span><span class="si">{</span><span class="n">hw_check_pass</span><span class="si">}</span><span class="s1">&amp;err_msg=</span><span class="si">{</span><span class="n">err_msg</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">err_msg</span> <span class="o">+=</span> <span class="s1">&#39;重启&#39;</span> <span class="k">if</span> <span class="n">err_msg</span> <span class="k">else</span> <span class="s1">&#39;关闭&#39;</span>

    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;[</span><span class="si">{</span><span class="n">pid</span><span class="si">}</span><span class="s2">] 任务 </span><span class="si">{</span><span class="n">err_msg</span><span class="si">}</span><span class="s2"> 请求</span><span class="si">{</span><span class="s1">&#39;成功&#39;</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">res</span><span class="o">.</span><span class="n">status_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">200</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">res</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s1">&#39;success&#39;</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">&#39;失败&#39;</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="bind_hf_except_hook"><a class="viewcode-back" href="../../../../api/client.html#hfai.client.bind_hf_except_hook">[docs]</a><span class="k">def</span> <span class="nf">bind_hf_except_hook</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    该函数用于将 Process 类绑定异常 hook，在子进程发生异常时通知 server 将其强行关闭，并启动自我检查，发现硬件故障重启该任务</span>

<span class="sd">    Args:</span>
<span class="sd">        f (class): 传进来的 Process 类</span>

<span class="sd">    Examples:</span>

<span class="sd">        &gt;&gt;&gt; from hfai.client import bind_hf_except_hook</span>
<span class="sd">        &gt;&gt;&gt; from torch.multiprocessing import Process</span>
<span class="sd">        &gt;&gt;&gt; bind_hf_except_hook(Process)</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">my_except_hook</span><span class="p">(</span><span class="n">e</span><span class="p">:</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">ttype</span><span class="p">,</span> <span class="n">tvalue</span><span class="p">,</span> <span class="n">ttraceback</span><span class="p">):</span>
        <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">IS_SIMULATE</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;[</span><span class="si">{</span><span class="n">pid</span><span class="si">}</span><span class="s1">] 模拟调用到了 hfai 的异常绑定&#39;</span><span class="p">)</span>
            <span class="k">raise</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;MARSV2_TASK_TYPE&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;training&#39;</span><span class="p">:</span>
            <span class="k">raise</span>

        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">format_exception</span><span class="p">(</span><span class="n">ttype</span><span class="p">,</span> <span class="n">tvalue</span><span class="p">,</span> <span class="n">ttraceback</span><span class="p">)))</span>
        <span class="n">self_health_check</span><span class="p">(</span><span class="n">pid</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">try_except</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">handle_problems</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">my_except_hook</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="o">*</span><span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">())</span>

        <span class="k">return</span> <span class="n">handle_problems</span>

    <span class="nb">setattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;run&#39;</span><span class="p">,</span> <span class="n">try_except</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;run&#39;</span><span class="p">)))</span></div>


<span class="n">create_experiment_v2</span> <span class="o">=</span> <span class="n">create_experiment</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;self_health_check&#39;</span><span class="p">,</span> <span class="s1">&#39;bind_hf_except_hook&#39;</span><span class="p">,</span> <span class="s1">&#39;create_experiment_v2&#39;</span><span class="p">]</span>
</pre></div>

              </article>
              
            </div>
            <footer>
  

  <hr>

  <div role="contentinfo">
    <p>
      &copy; Copyright 2023, High-Flyer.

    </p>
  </div>
  
  <div>
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a
      href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the
      Docs</a>.
  </div>
   

</footer>
          </div>
        </div>

        <div class="pytorch-content-right" id="pytorch-content-right">
          <div class="pytorch-right-menu" id="pytorch-right-menu">
            <div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
              
            </div>
          </div>
        </div>
    </section>
  </div>

  


  

  
  <script type="text/javascript" id="documentation_options" data-url_root="../../../../"
    src="../../../../_static/documentation_options.js"></script>
  <script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
  <script src="../../../../_static/jquery.js"></script>
  <script src="../../../../_static/underscore.js"></script>
  <script src="../../../../_static/doctools.js"></script>
  <script src="../../../../_static/js/vue.global.prod.js"></script>
  <script src="../../../../_static/js/element-plus.full.js"></script>
  

  

  <script type="text/javascript" src="../../../../_static/js/vendor/popper.min.js"></script>
  <script type="text/javascript" src="../../../../_static/js/vendor/bootstrap.min.js"></script>
  <script src="../../../../_static/external/list.min.js"></script>
  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
    jQuery(function () {
      SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

  <!-- Begin Footer -->

  <!-- <div class="container-fluid docs-tutorials-resources" id="docs-tutorials-resources">
  </div> -->

  <!-- End Footer -->

  <!-- Begin Mobile Menu -->

  <div class="mobile-main-menu">
    <div class="container-fluid">
      <div class="container">
        <div class="mobile-main-menu-header-container">
          <a class="header-logo" href="" aria-label="OpenMMLab"></a>
          <a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
        </div>
      </div>
    </div>

    <div class="mobile-main-menu-links-container">
      <div class="main-menu">
        <ul>
      </div>
    </div>
  </div>

  <!-- End Mobile Menu -->

  <script type="text/javascript" src="../../../../_static/js/vendor/anchor.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function () {
      mobileMenu.bind();
      mobileTOC.bind();
      pytorchAnchors.bind();
      sideMenus.bind();
      scrollToAnchor.bind();
      highlightNavigation.bind();
      mainMenuDropdown.bind();
      filterTags.bind();

      // Add class to links that have code blocks, since we cannot create links in code blocks
      $("article.pytorch-article a span.pre").each(function (e) {
        $(this).closest("a").addClass("has-code");
      });
    })
  </script>
</body>

</html>